package top.lanscarlos.ashcraft;

import com.google.gson.Gson;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import top.lanscarlos.ashcraft.pojo.User;

/**
 * Ashcraft
 * top.lanscarlos.ashcraft
 *
 * @author Lanscarlos
 * @since 2022-05-25 13:31
 */
public class HibernateTest {
    private static SessionFactory sessionFactory;
    public static void main(String[] args) {
        sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            User user = new User("李玉兰", "11123", null, "你瞅啥？", 1, 0.0, "你家", "11sa3124514");
            session.save(user);
//            User user = session.get(User.class, 3);
            System.out.println(user);
            Gson gson = new Gson();
            System.out.println(gson.toJson(gson.toJsonTree(user)));
//            transaction.commit();
        } catch (Exception e) {
            if (transaction != null) transaction.rollback();
            e.printStackTrace();
        }
    }
}
